import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

router.beforeEach((to, from, next) => {
  // 登录验证
  const token = localStorage.getItem('token')
  if (to.path !== '/login' && !token) {
    next('/login')
  } else {
    // 设置页面背景色
    if (to.path === '/home') {
      document.body.classList.add('home-page')
    } else {
      document.body.classList.remove('home-page')
    }
    next()
  }
})

export default router
